<?php
namespace app\index\controller;
use app\index\controller\Home;
class Index extends Home
{

    public function _initialize() {
        parent::_initialize();
        $this->uid = session('wx_uid');
    }
    /**
     * [index description]
     */
    public function index()
    {
        //权限管理，招标这块不能看 9 10 11 需要权限
        $map['status'] = 1;
        $map['category'] = input('category');
        $pid = db('category')->where('id',$map['category'])->find();
        $category = db('category')->where('pid',$pid['pid'])->select();

        //$data = db('post')->where($map)->order('id DESC')->select();
        $map1=array('post.uid'=>$this->uid,'post.status'=>1);
        $data =db('post')->alias('p')
                ->join('authentication a','p.uid = a.uid','LEFT')
                ->where($map1)->order('p.id DESC')
                ->field('p.*,a.job')
                ->select();


        $authMap = array('status'=>1,'uid'=>$this->uid);
        $userAuth = db('authentication')->where($authMap)->find();
        if(empty($userAuth)){
            if(in_array(input('category'),array(9,10,8))){
                 $this->error('认证过后才可以查看此模块的内容','user/authentication');
            }
            
        }
        $userAuth ? $this->assign('auth',1) : $this->assign('auth',0);//权限认证
        foreach ($data as $key => $value) {
            $data[$key]['nickname'] = db('user')->where('id',$value['uid'])->value('nickname');
        }

        //设置栏目隐藏 
         if (in_array(input('category'),array(4,5,6,8,9,11,13,14))) {
            $this->assign('a',0);  
        }else{
            $this->assign('a',1);
        }
       


        $this->assign('data',$data);
        $this->assign('category',$category);
        $this->assign('pid',$pid['pid']);
        $this->assign('id',input('category'));
        return $this->fetch();
    }
    /**
     * 发布
     */
    public function edit()
    {
        if(request()->isPost()){
             $param = request()->param();
             if($param['__token__']  !== $_SESSION['think']['__token__']){
                return json(['code'=>0,'message'=>'请勿反复提交']);
             }
             //获取上次发言时间,5分钟内禁止发
             $lastTime= db('post')->where('uid',$this->uid)->order('id DESC')->limit(1)->value('create_time');
             $time = time()-$lastTime;
             if($time<60*2){return json(['code'=>0,'message'=>'亲，两次发布时间要大于2分钟哦']);}
             $data['uid'] = $this->uid ;
             $data['title'] = $param['title'];
             $data['content'] = $param['content'];
             $data['category'] = $param['category'];
             $data['create_time'] = time();
             if($param['category'] == -1){//判断自定义标签
                //判断是否重复
                $val = db('category')->where('name',$param['tag'])->find();
                if($val){
                    $data['category'] = $val['id'];
                }else{
                    $tag['name'] = $param['tag'];
                    $tag['pid'] = -1;
                    $tag['status'] = 1;
                    $data['category'] = db('category')->insertGetId($tag);
                }
             }
             $id =db('post')->insertGetId($data);
             if($id){
                session('__token__',null);
               return json(['code'=>$id,'message'=>'发布成功']);
             }else{
                return json(['code'=>0,'message'=>'发布失败']);
             }
        }else{
             $pid = db('category')->where('pid',input('pid'))->find();
             $category = db('category')->where('pid',$pid['pid'])->select();
             $cid = db('category')->where('id',input('pid'))->find();
             $tips = db('tips')->where('cid',$cid['pid'])->find();
             $this->assign('tips',$tips);
             $this->assign('category',$category);
             return $this->fetch();
        }
    }
    /**
     * 详情
     */
    public function detail()
    {

        $id = input('param.id');

        //$data = db('post')->where('id',$id)->find();
        $data =db('post')->alias('p')
                ->join('authentication a','p.uid = a.uid','LEFT')
                ->field('p.*,a.job')
                ->find();

        $inc  = db('post')->where('id',$id)->setInc('browse');
        $map['uid'] = $this->uid;
        $map['pid'] = $id;
        $isZan = db('zan')->where($map)->find();
        if($isZan){
            $this->assign('zan',1);
        }else{
            $this->assign('zan',0);
        }
        $record = db('record')->where($map)->find();
        $map['create_time'] = time();
        if(empty($record)){
            $rid = db('record')->insertGetId($map);
        }else{
            $rid = db('record')->where('id',$record['id'])->update($map);
        }
        $commnetlist = db('comment')->where('pid',$id)->select();
        foreach ($commnetlist as $k => $v) {
            $commnetlist[$k]['username'] = db('user')->where('id',$v['uid'])->value('nickname');
        }
        $data['qrcode'] = db('user')->where('id',$this->uid)->value('qrcode');
        $this->assign('data',$data);
        $this->assign('comment',$commnetlist);
        return $this->fetch();
    }

    /**
     * 热门
     */
    public function hot()
    {
        $data = db('post')->where('status',1)->order('browse DESC')->select();
        $this->assign('data',$data);
        return $this->fetch();
    }

    /**
     * 点赞
     */
    public function zan()
    {
        if(request()->isPost()){
             $param = request()->param();
             $data['uid'] = $this->uid ;
             $data['pid'] = input('param.id');
             $v = db('zan')->insert($data);
             $inc  = db('post')->where('id',$data['pid'])->setInc('zan');
             if($v && $inc){
               return json(['code'=>1,'message'=>'点赞成功']);
             }else{
                return json(['code'=>0,'message'=>'点赞失败']);
             }
        }

        if(request()->isDelete()){
             $param = request()->param();
             $data['uid'] = $this->uid ;
             $data['pid'] = input('param.id');
             $del = db('zan')->where($data)->delete();
             $dec  = db('post')->where('id',$data['pid'])->setDec('zan');
             if($del && $dec){
               return json(['code'=>1,'message'=>'取消点赞成功']);
             }else{
                return json(['code'=>0,'message'=>'取消点赞失败']);
             }
        }

    }
    /**
     * 我的
     */
    public function my()
    {

      //我看过的
        $data = db('record') 
              ->join('post', 'record.pid = post.id')
              ->join('authentication', 'authentication.uid = post.uid','left')
              ->where('record.uid',$this->uid)
              ->order('record.create_time DESC')
              ->select();
              //$data=db('record')->join('post','record.pid=post.id')->where('record.uid',$this->uid)->order('record.create_time DESC')->select();
        //dump($data);exit;
        
        //我的发布
        //$myPost = db('post')->where(array('uid'=>$this->uid,'status'=>1))->order('id DESC')->select();

        $map=array('post.uid'=>$this->uid,'post.status'=>1);
        $myPost =db('post')->alias('p')
                ->join('authentication a','p.uid = a.uid','LEFT')
                ->where($map)->order('p.id DESC')
                ->field('p.*,a.job')
                ->select();

        
        $this->assign('data',$data);
        $this->assign('myPost',$myPost);
        return $this->fetch();
    }


    /**
     * 菜单
     */
    public function menu()
    {
       /* $parent = db('category')->where('pid',0)->select();
        $son = db('category')->where('pid','gt',0)->select();
        foreach ($parent as $k => $v) {
            foreach ($son as $key => $val) {
                if($val['pid'] == $v['id']){
                    $parent[$k]['son'][] = $val;
                }
            }
        }
        $this->assign('menu',$parent);*/
        return $this->fetch();
    }















}
